#include "MessageBox.h"
#include "Widgets/Window.h"
#include "Widgets/TextWidget.h"
#include "Widgets/ButtonWidget.h"
#include "Widgets/WindowManager.h"
#include "Widgets/Editor/DragWindowArea.h"

#include "Widgets/Positioners/HorizontalPositionerWidget.h"

// RMessageBox::RMessageBox()
// {
//     bIsModalWindow=true;
    
//     MessageWidget=NewWidget<RTextWidget>(GetRootWidget());
//     MessageWidget->Anchor.HorizontalCenterReference=GetRootWidget();
//     MessageWidget->Anchor.VerticalCenterReference=GetRootWidget();

//     auto OkButton=NewWidget<RButtonWidget>(GetRootWidget());
//     OkButton->Anchor.RightReference=GetRootWidget();
//     OkButton->Anchor.RightOffset=20;
//     OkButton->Anchor.BottomReference=GetRootWidget();
//     OkButton->Anchor.BottomOffset=20;
//     OkButton->Text=(U"Ok");

//     OkButton->

// }


WH::EMessageBoxResult WH::ShowMessageBox(const CString &InWindowTitle, const CString &InMessage, WH::EMessageBoxType InType)
{
    auto Window=NewObject<RWindow>();
    Window->HideSystemBorder();
    Window->SetWindowTitle(InWindowTitle);
    Window->SetWindowSize(SIntPoint(300,150));
    Window->MoveToScreenCenter();
    Window->GetRootWidget()->FillColor=SColor::FromHexRGB(0x333333);


    auto DragWindowArea=NewWidget<RMouseDragWindowArea>(Window->GetRootWidget());
    DragWindowArea->Height=20;
    DragWindowArea->Anchor.HorizontalFill(Window->GetRootWidget());

    auto TitleBackground=NewWidget<RRectangleWidget>(DragWindowArea);
    TitleBackground->Anchor.Fill(DragWindowArea);
    TitleBackground->FillColor=SColor::FromHexRGB(0x444444);

    auto TitleWidget=NewWidget<RTextWidget>(DragWindowArea);
    TitleWidget->Anchor.VerticalFill(DragWindowArea);
    TitleWidget->Text=InWindowTitle;
    TitleWidget->X=15;
    TitleWidget->VerticalAlignment=ETextVerticalAlignment::Center;

    auto MessageBackground=NewWidget<RWidget>(Window->GetRootWidget());
    auto ButtonPanel=NewWidget<RHorizontalPositionerWidget>(Window->GetRootWidget());
    MessageBackground->Anchor.HorizontalFill(Window->GetRootWidget());
    MessageBackground->Anchor.TopReference=TitleBackground;
    MessageBackground->Anchor.TopReferenceEdge=EVerticalWidgetEdge::Bottom;
    MessageBackground->Anchor.BottomReference=ButtonPanel;
    MessageBackground->Anchor.BottomReferenceEdge=EVerticalWidgetEdge::Top;


    auto MessageWidget=NewWidget<RTextWidget>(MessageBackground);
    MessageWidget->Anchor.CenterIn(MessageBackground);
    RTextWidgetWeakPtr MessageWidgetWeak=MessageWidget;
    MessageWidget->Width=200;
    MessageWidget->Height=[MessageWidgetWeak]()
    {
        return MessageWidgetWeak->GetTextSize().Y;
    };
    MessageWidget->Text=InMessage;
    MessageWidget->Wrap=ETextWrapMethod::WrapByWidthAndAtLineEnd;


   
    ButtonPanel->Height=20;
    ButtonPanel->Anchor.BottomReference=Window->GetRootWidget();
    ButtonPanel->Anchor.BottomOffset=10;
    ButtonPanel->Anchor.RightReference=Window->GetRootWidget();
    ButtonPanel->Anchor.RightOffset=10;

    auto WeakWindow=RWindowWeakPtr(Window);
    auto WeakButtonPanel=RHorizontalPositionerWidgetWeakPtr(ButtonPanel);
    WH::EMessageBoxResult Result;

    static float ButtonWidth=50;
    static float ButtonSpacing=5;
    ButtonPanel->Spacing=ButtonSpacing;
    auto CreateButton=[WeakWindow,WeakButtonPanel,&Result](WH::EMessageBoxResult ClickResult,const CString& InText)->void
    {
        auto Button=NewWidget<RButtonWidget>(WeakButtonPanel);
        Button->Width=ButtonWidth;
        Button->Height=20;
        Button->SetHovorColor(SColor::FromHexRGB(0x444444),SColor::Green);
        Button->Text=InText;
        Button->OnClicked.BindLambda([WeakWindow,&Result,ClickResult](const SMouseButtonEvent &)->bool{
            WeakWindow->Close();
            Result=ClickResult;
            return true;
        }); 
    };
  
    switch(InType)  
    {
        case WH::EMessageBoxType::Ok:
        {
            CreateButton(WH::EMessageBoxResult::Ok,U"Ok");
            ButtonPanel->Width=ButtonWidth;
            break;
        }
        case WH::EMessageBoxType::OkCancel:
        {
            CreateButton(WH::EMessageBoxResult::Ok,U"Ok");
            CreateButton(WH::EMessageBoxResult::Cancel,U"Cancel");
            ButtonPanel->Width=ButtonWidth*2+ButtonSpacing;
            break;
        }
        case WH::EMessageBoxType::YesNo:
        {
            CreateButton(WH::EMessageBoxResult::Yes,U"Yes");
            CreateButton(WH::EMessageBoxResult::No,U"No");
            ButtonPanel->Width=ButtonWidth*2+ButtonSpacing;
            break;
        }
        case WH::EMessageBoxType::YesNoCancel:
        {
            CreateButton(WH::EMessageBoxResult::Yes,U"Yes");
            CreateButton(WH::EMessageBoxResult::No,U"No");
            CreateButton(WH::EMessageBoxResult::Cancel,U"Cancel");
            ButtonPanel->Width=ButtonWidth*3+ButtonSpacing*2;
            break;
        }
    }

    Window->Show();
    RWindowManager::Get()->MakeWindowModal(Window);

    return Result;

}
